import Director from '../director'
import store from '../base/store'
import Sprite from '../base/sprite'

/**
 * 地板精灵
 */
export class FloorSprite extends Sprite {
  constructor() {
    const image = Sprite.getMap('sprites')
    const floor = Sprite.getSpritesData('floor')
    super(
      image,
      floor.sx,
      floor.sy,
      floor.sw,
      floor.sh,
      0,
      store.canvas.height - floor.sh,
      store.canvas.width * 2,
      floor.sh
    )
  }

  /**
   * 绘制函数：地板循环向左移动
   * @returns 无
   */
  public override draw(): void {
    // 1.当前地板图在canvas的x坐标
    this.x += Director.getInstance().speed
    // 2.只要 x坐标 > 地板图宽度 - 视口宽度 则 地板图会和背景出现间隙 因此重置为0
    if (this.x > this.w - store.canvas.width) {
      this.x = 0
    }
    super.draw({ x: -this.x })
  }
}
